package com.project.service.impl;

import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class StudentServiceImpl extends BaseService implements IStudentService {
    @Override
    public int add(StudentBean student) {
        SqlSession session = this.getSession();
        //使用代理方式，得到Mapper接口实现类对象
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(student);
        session.commit();
        session.close();
        return student.getId();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delMore(idArray);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, String tel) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(id, tel);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findAll() {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list =  mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean student = mapper.findById(id);
        session.close();
        return student;
    }

    @Override
    public List<StudentBean> findByName(String name) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list =  mapper.findByName("%"+name+"%");
        session.close();
        return list;
    }

    public static void main(String[] args) {
        IStudentService service = new StudentServiceImpl();
//        service.add(new StudentBean("aaa", "234324", LocalDate.parse("1992-03-04")));
//        System.out.println(service.add(new StudentBean("bbb", "324324324", LocalDate.parse("1992-03-04"))));
//        service.del(13);
//        service.update(12, "12321321");
//        service.delMore(new int[]{10,12});
//        System.out.println(service.findAll());
        System.out.println(service.findById(1));
        System.out.println(service.findByName("张"));
    }
}
